export function formatTime (seconds: number): string {
  seconds = Math.floor(seconds)
  const hours: number = Math.floor(seconds / 3600)
  const minutes: number = Math.floor((seconds - (hours * 3600)) / 60)
  const remainingSeconds: number = seconds - (hours * 3600) - (minutes * 60)

  const formattedHours: string = (hours === 0) ? '' : ((hours < 10) ? '0' + hours : hours.toString())
  const formattedMinutes: string = (minutes < 10) ? '0' + minutes : minutes.toString()
  const formattedSeconds: string = (remainingSeconds < 10) ? '0' + remainingSeconds : remainingSeconds.toString()

  let timeString: string = formattedMinutes + ':' + formattedSeconds

  if (formattedHours !== '') {
    timeString = formattedHours + ':' + timeString
  }

  return timeString
}

export function downloadFile (url: string, filePath: string): void {
  fetch(url)
    .then(async response => await response.blob())
    .then(blob => {
      const downloadLink = document.createElement('a')
      downloadLink.href = window.URL.createObjectURL(blob)
      downloadLink.download = filePath
      document.body.appendChild(downloadLink)
      downloadLink.click()
      document.body.removeChild(downloadLink)
    })
    .catch(error => {
      console.error('下载文件失败', error)
    })
}
